extends Node

@export var map_layer:TileMapLayer
@onready var input_manager: InputManager = %InputManager
@onready var move_manager: MoveManager = %MoveManager
@onready var attack_manager: AttackManager = %AttackManager


func _on_base_state_state_input(event: InputEvent) -> void:
	if event and event.is_action_pressed("left_mouse"):
		var colliders: Array[Dictionary] = input_manager.mouse_shot()
		if colliders.is_empty():
			#process_no_collision()
			return
		
		var tile_colliders: Array = colliders.filter(func(c): return c.collider is TileMapLayer)
		#if cur_action_status == ACTION_STATUS.TO_MOVING and not tile_colliders.is_empty():
			#MOVE_MANAGER.move_character_to(get_top_cell(tile_colliders))
			#return
		var hero_colliders: Array = colliders.filter(func(c): return c.collider is Player)
		if not hero_colliders.is_empty():
			print("英雄点击")
			var player_unit:Player = hero_colliders[0].collider
			input_manager.player_chosen = player_unit
			%GameStateMachine.send_event("click_unit")
			return



func _on_wait_to_move_state_input(event: InputEvent) -> void:
	right_mouse_input(event)
	if not input_manager.player_chosen:
		return	
	var cur_mouse_cell:Vector2i = TileMapUtil.convert_globle_p_2_layer_cell(get_parent().get_parent().get_global_mouse_position(), map_layer)
	var start_cell: Vector2i = TileMapUtil.convert_globle_p_2_layer_cell(input_manager.player_chosen.position, map_layer)
	move_manager.show_path_preview(cur_mouse_cell, start_cell)
	if event and event.is_action_pressed("left_mouse"):
		if not move_manager.can_move(cur_mouse_cell):
			return
		move_manager.move_target_cell = cur_mouse_cell
		%GameStateMachine.send_event("selectEffectiveMoveCell")


func right_mouse_input(event: InputEvent) -> void:
	if event and event.is_action_pressed("right_mouse"):
		%GameStateMachine.send_event("toGameBase")


func _on_select_unit_action_state_input(event: InputEvent) -> void:
	right_mouse_input(event)


func _on_target_state_input(event: InputEvent) -> void:
	right_mouse_input(event)
